import {nextTick} from 'vue';
import {useGlobalStoreHook} from "@/store/global";

const globalStore = useGlobalStoreHook();

export function beforeEach(to, from) {
    const adminMain = document.querySelector('#admin-ui-main');
    if (!adminMain) {
        return false;
    }
    globalStore.updateViewTags({
        fullPath: from.fullPath,
        scrollTop: adminMain.scrollTop
    });
}

export function afterEach(to) {
    const adminMain = document.querySelector('#admin-ui-main');
    if (!adminMain) {
        return false;
    }
    nextTick(() => {
        const beforeRoute = useGlobalStoreHook().getViewTags.filter((v) => v.fullPath === to.fullPath)[0];
        if (beforeRoute) {
            adminMain.scrollTop = beforeRoute.scrollTop || 0;
        }
    });
}
